<?php
	namespace Admin\Controller;
	use Think\Controller;
/**
 * 前台导航菜单 管理控制器
 */
 class WmenuController extends  IsLoginController{
 	/*
	 * 管理首页
	 */
	 public function index(){
	 	$uid = $_SESSION['uid'];
	 	$menu = M("wxnavigation")->where("wxnuid = '{$uid}'")->select();
		//实例化扩展类 栏目树
		$list = new \Org\Util\Data;
		$menuList = $list::tree($menu,'wxnname','id','wxnfid');
		$this->assign('menuList',$menuList);
	 	$this->display();
	 }
	 
	 /**
	  * 添加菜单
	  */
	  public function add(){
	  	if(IS_POST){
	 		$nav = D('wxnavigation');
			if(!$nav->addNav()){
				$this->error($nav->getError());
				return false;
			}
			$this->success('添加成功',U('Admin/wmenu/index'));
	 	}
		$uid = $_SESSION['uid'];
		//不是顶级栏目不能选择
		$cid = M("wxnavigation")->where("wxnuid = '{$uid}' and wxnfid != 0")->select();
		$newcid = '';
		foreach($cid as $k =>$v ){
			$newcid[$k] = $v['id'];
		}
		$newcid = array_filter($newcid);
		$this->assign('cid',$newcid);
		$menu = M("wxnavigation")->where("wxnuid = '{$uid}'")->select();
		//实例化扩展类 栏目树
		$list = new \Org\Util\Data;
		$menuList = $list::tree($menu,'wxnname','id','wxnfid');
		$this->assign('menuList',$menuList);
	  	$this->display();
	  }
	 
	 /**
	  * 修改菜单
	  */
	 public function edit(){
	 	$nid = I('get.nid');
	 	if(IS_POST){
	 		$nav = D('wxnavigation');
			if(!$nav->editNav()){
				$this->error($nav->getError());
				return false;
			}
			$this->success('修改成功',U('Member/wmenu/index'));
	 	}
	 	$uid= $_SESSION['uid'];
		$nidData = M('wxnavigation')->where("id='{$nid}'")->find();	
		$this->assign('nidData',$nidData);
		//不可选择栏目  自身和自身子栏目
		$selfid =  $nidData['id'];
		$cid = M('wxnavigation')->where("wxnfid='{$selfid}' and  wxnuid ='{$uid}'")->field('id')->select(); //查询子栏目id
		$newcid = '';
		foreach($cid as $k =>$v){
			$newcid = $newcid .','.$v['id'];			
		}
		$newcid = explode(',', $newcid);
		$newcid = array_filter($newcid);
		$this->assign('cid',$newcid);
		
	 	$menu = M("wxnavigation")->where("wxnuid ='{$uid}'")->select();
		//实例化扩展类 栏目树
		$list = new \Org\Util\Data;
		$menuList = $list::tree($menu,'wxnname','id','wxnfid');
		$this->assign('menuList',$menuList);
	 	$this->display();
	 }
	 
	 /**
	  * 删除菜单
	  */
	public function del(){
		if(IS_GET){
	 		$nav = D('wxnavigation');
			if(!$nav->del()){
				$this->error($nav->getError());
				return false;
			}
			$this->success('删除成功',U('Admin/Wmenu/index'));
		}
	}
	  
	  
	  
	 /**
	  * 生成菜单
	  */
	  public function CreateMenu(){
	  		$uid= I('get.uid');
			//查询当前用户的所有一级栏目
			$Data = M('wxnavigation')->where("wxnuid = '{$uid}' and wxnfid = 0")->limit(3)->select();
			if(!$Data){
				$this->error('栏目为空，请添加后再执行生成');
			}
			//循环一级栏目下的二级栏目
			foreach($Data as $k => $v){
				$fatherType =$v['wxntype'];   // 0跳转链接  1点击事件 2扫码事件 3扫码提示 4系统拍照 5拍照或者相册图 6微信相册发图 7 发送位置
				if($fatherType == 0 ){
					$type = 'view';
					$key  = urlencode($v['wxnurl']);
				}if($fatherType == 1 ){
					$type = 'click';
					$key  = 'click_event1';
				}if($fatherType == 2 ){
					$type = 'scancode_push';
					$key  = 'scancode_push_event1';
				}if($fatherType == 3 ){
					$type = 'scancode_waitmsg';
					$key  = 'scancode_waitmsg_event1';
				}if($fatherType == 4 ){
					$type = 'pic_sysphoto';
					$key  = 'pic_sysphoto_event1';
				}if($fatherType == 5 ){
					$type = 'pic_photo_or_album';
					$key  = 'pic_photo_or_album_event1';
				}if($fatherType == 6 ){
					$type = 'pic_weixin';
					$key  = 'pic_weixin_event1';
				}if($fatherType == 7){
					$type = 'location_select';
					$key  = 'location_select_event1';
				}
				$menu['button'][$k]['type'] =$type;
				$menu['button'][$k]['name'] =urlencode($v['wxnname']);
				if($type == 'view'){
					$menu['button'][$k]['url'] =$key;	
				}else{
					$menu['button'][$k]['key'] =$key;
				}
				
				$cid = $v['id'];
				$Data[$k]['second'] = M('wxnavigation')->where("wxnuid = '{$uid}' and wxnfid = '{$cid}'")->limit(5)->select();
				if($Data[$k]['second']){
					/**
					 * 如果有二级菜单
					 */
					 $menu['button'][$k] = '';
					 $menu['button'][$k]['name'] =urlencode($v['wxnname']);
					 $menu['button'][$k]['sub_button'] = array();
					foreach($Data[$k]['second'] as $kk =>$vv){
						
						$fatherType =$vv['wxntype'];   // 0跳转链接  1点击事件 2扫码事件 3扫码提示 4系统拍照 5拍照或者相册图 6微信相册发图 7 发送位置
						if($fatherType == 0 ){
							$type = 'view';
							$key  = urlencode($vv['wxnurl']);
						}if($fatherType == 1 ){
							$type = 'click';
							$key  = 'click_event1';
						}if($fatherType == 2 ){
							$type = 'scancode_push';
							$key  = 'scancode_push_event1';
						}if($fatherType == 3 ){
							$type = 'scancode_waitmsg';
							$key  = 'scancode_waitmsg_event1';
						}if($fatherType == 4 ){
							$type = 'pic_sysphoto';
							$key  = 'pic_sysphoto_event1';
						}if($fatherType == 5 ){
							$type = 'pic_photo_or_album';
							$key  = 'pic_photo_or_album_event1';
						}if($fatherType == 6 ){
							$type = 'pic_weixin';
							$key  = 'pic_weixin_event1';
						}if($fatherType == 7){
							$type = 'location_select';
							$key  = 'location_select_event1';
						}
						if($type == 'view'){
							$son =array(
								'type' =>$type,
								'name' =>urlencode($vv['wxnname']),
								'url' =>$key
							);
						}else{
							$son =array(
								'type' =>$type,
								'name' =>urlencode($vv['wxnname']),
								'key' =>$key
							);
						}

						array_push($menu['button'][$k]['sub_button'],$son);
						
					}
				}
			}
			//转json格式
			$menu = json_encode($menu);
			//将中文代码回转
			$data = urldecode($menu);
			 		/**
					 * 调用APi类 创建菜单
					 */


		  $newWechat = $this->newWechat($uid);
		  \Think\Log::record('$api' . json_encode($newWechat) , 'INFO');
		list($wechat,$api) = $newWechat;
		\Think\Log::record('$api' . json_encode($api) , 'INFO');

					
					
////////////////  用户信息查询调试
//					list($err,$res) =$api->get_user_info('oftNbt_rdVlZbZtxmp9HE4TifrwU');
//					p($err);p($res);
//					if($res){
//						$res = json_decode(json_encode($res),ture);
//						$data = $res;
//					}
//p($data);p($res);$slide
/////////////////

			list( $err,$res) = $api->create_menu($data);
			$save['wxmenu'] = $data	;

		  	//将编码写入数据库
		  	M('wxapi')->where("wxuid  = '{$uid}' ")->save($save);
			if($res){
				$this->success('菜单生成完毕',U('Admin/wmenu/index'));
			}else{
				$this->error("生成菜单失败！ {$err->errcode}:  {$err->errmsg} ");
			}
	  }
	  
	/**
	 * 删除菜单
	 */
	public function delMenu(){
		/**
		 * 调用APi类 删除菜单
		 */
			$uid = $_SESSION['uid'];			//获取用户id
	        // 保存access_token //初始化缓存设置
	        S(array(
	            'type'=>'file', 			//缓存方式文件类型
	            'prefix'=>'weixin_'.$uid.'_',			//缓存前缀
	            'expire'=>7200				//缓存时间
	        ));

		$newWechat = $this->newWechat();
		list($wechat,$api) = $newWechat;
		\Think\Log::record('$api' . json_encode($api) , 'INFO');

			list($err, $res) = $api->delete_menu();

			if($res){
				//将编码写入数据库
				$this->success('菜单删除完成',U('Admin/wmenu/index'));
			}else{
				$this->error("生成菜单失败！{$err->errcode}:  {$err->errmsg}  ");
			}
	}
	
	
	
	
 }
